package com.hzy.fivepalace.admin.controller;

import com.hzy.fivepalace.admin.annotation.Login;
import com.hzy.fivepalace.admin.controller.base.BaseController;
import com.hzy.fivepalace.admin.service.AdminOrderService;
import com.hzy.fivepalace.common.util.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/admin/order")
@Validated
@Api(tags = "订单服务")
@Login
public class AdminOrderController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(AdminOrderController.class);

    @Autowired
    private AdminOrderService orderService;

    /**
     * 获取会员开通记录
     * @param page
     * @param size
     * @param userId 用户id(可以查某个用户的)
     * @param vipType 会员类型(可以查某个类型的会员)
     * @param mobile 手机号(可以根据手机号搜索)
     * @param keyWord 关键字(可以匹配用户昵称和手机号)
     * @return
     */
    @PostMapping("/getVipByRecordsList")
    @ApiOperation(value = "获取会员开通记录")
    public TableDataInfo getVipByRecordsList(
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer page,
            @ApiParam(value = "分页大小", defaultValue = "10") @RequestParam(defaultValue = "100") Integer size,
            @ApiParam(value = "用户id(可以查某个用户的)") @RequestParam(value = "userId", required = false) Integer userId,
            @ApiParam(value = "会员类型(可以查某个类型的会员)") @RequestParam(value = "vipType", required = false) Integer vipType,
            @ApiParam(value = "手机号(可以根据手机号搜索)") @RequestParam(value = "mobile", required = false) String mobile,
            @ApiParam(value = "关键字(可以匹配用户昵称和手机号)") @RequestParam(value = "keyWord", required = false) String keyWord
    ) {
        return orderService.getVipByRecordsList(page, size,userId,vipType,mobile,keyWord);
    }

    /**
     * 订单列表
     *
     * @param userId
     * @param phone
     * @param name
     * @param orderSn
     * @param status  状态:为空就查所有,新增-1状态(查售后订单列表)
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/orderList")
    @ApiOperation(value = "订单列表")
    public TableDataInfo orderList(
            @ApiParam(value = "用户id:可对单个用户查询,为空就查所有") @RequestParam(value = "userId", required = false) Integer userId,
            @ApiParam(value = "收货号码") @RequestParam(value = "phone", required = false) String phone,
            @ApiParam(value = "收货人名称") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "订单编号") @RequestParam(value = "orderSn", required = false) String orderSn,
            @ApiParam(value = "状态:为空就查所有,新增-1状态(查售后订单列表)") @RequestParam(value = "status", required = false) Integer status,
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer page,
            @ApiParam(value = "分页大小", defaultValue = "10") @RequestParam(defaultValue = "10") Integer size) {

        return orderService.getUserOrderList(page, size, status, phone, userId, name, orderSn);
    }

    /**
     * 订单详情
     *
     * @param orderId
     * @return
     */
    @PostMapping("/orderDetails")
    @ApiOperation(value = "订单详情")
    public Object orderDetails(
            @ApiParam(value = "订单id") @RequestParam(value = "orderId") @NotNull Integer orderId) {

        return orderService.orderDetails(orderId);
    }

    /**
     * 查看物流
     *
     * @param orderId
     * @return
     */
    @PostMapping("/queryLogistics")
    @ApiOperation(value = "查看物流")
    public Object queryLogistics(
            @ApiParam(value = "订单id") @RequestParam(value = "orderId") @NotNull Integer orderId
    ) {

        return orderService.queryLogistics(orderId);
    }

    /**
     * 查看物流
     *
     * @param orderId
     * @return
     */
    @PostMapping("/queryReturnLogistics")
    @ApiOperation(value = "查看退货物流")
    public Object queryReturnLogistics(
            @ApiParam(value = "订单id") @RequestParam(value = "orderId") @NotNull Integer orderId
    ) {

        return orderService.queryReturnLogistics(orderId);
    }


    /**
     * 订单发货
     *
     * @param orderId
     * @param shipCode
     * @param shipSn
     * @return
     */
    @PostMapping("/delivery")
    @ApiOperation(value = "订单发货")
    public Object delivery(
            @ApiParam(value = "订单id") @RequestParam(value = "orderId") @NotNull Integer orderId,
            @ApiParam(value = "快递公司编码") @RequestParam(value = "shipCode") @NotBlank String shipCode,
            @ApiParam(value = "快递单号") @RequestParam(value = "shipSn") @NotBlank String shipSn
    ) {

        return orderService.delivery(orderId, shipCode, shipSn);
    }

    /**
     * 单独退款
     *
     * @param orderId
     * @param refundDesc
     * @return
     */
    @PostMapping("/orderRefund")
    @ApiOperation(value = "单独退款")
    public Object orderRefund(
            @ApiParam(value = "订单id") @RequestParam(value = "orderId") @NotNull Integer orderId,
            @ApiParam(value = "退款原因,必填") @RequestParam(value = "refundDesc") @NotBlank String refundDesc
    ) {

        return orderService.orderRefund(orderId, refundDesc);
    }

    /**
     * 确认收货并退款
     *
     * @param orderId
     * @return
     */
    @PostMapping("/afterSaleRefund")
    @ApiOperation(value = "确认收货并退款")
    public Object afterSaleRefund(
            @ApiParam(value = "订单id") @RequestParam(value = "orderId") @NotNull Integer orderId
    ) {

        return orderService.afterSaleRefund(orderId);
    }

    /**
     * 售后审核
     * @param orderId
     * @param disposeType
     * @param reason
     * @return
     */
    @PostMapping("/applyAfterSale")
    @ApiOperation(value = "售后审核")
    public Object applyAfterSale(
            @ApiParam(value = "订单id") @RequestParam(value = "orderId") @NotNull Integer orderId,
            @ApiParam(value = "处理方式:0拒绝,1仅退款,2退货退款") @RequestParam(value = "disposeType") @NotNull Integer disposeType,
            @ApiParam(value = "拒绝原因,处理方式为0必须填写") @RequestParam(value = "reason",defaultValue ="",required = false ) String reason

    ) {

        return orderService.applyAfterSale(orderId,disposeType,reason);
    }


    /**
     * 获取所有快递公司
     *
     * @return
     */
    @PostMapping("/getAllShipChannel")
    @ApiOperation(value = "获取所有快递公司")
    public Object getAllShipChannel() {

        return orderService.getAllShipChannel();
    }


}
